package com.WebMentorPro.system.service.impl;

import com.WebMentorPro.system.common.ErrorCode;
import com.WebMentorPro.system.exception.BusinessException;
import com.WebMentorPro.system.mapper.NotificationMapper;
import com.WebMentorPro.system.model.dto.notification.NotificationQueryAdminRequest;
import com.WebMentorPro.system.model.dto.notification.NotificationQueryRequest;
import com.WebMentorPro.system.model.entity.Notification;
import com.WebMentorPro.system.model.vo.NotificationVO;
import com.WebMentorPro.system.service.NotificationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bianxiaoyao
 * @description 针对表【notification(异科导师消息通知表)】的数据库操作Service实现
 * @createDate 2024-11-24 11:24:42
 */
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements NotificationService {

    @Resource
    private NotificationMapper notificationMapper;

    @Override
    public Page<NotificationVO> getQueryWrapper(Page<NotificationVO> pageParam, NotificationQueryRequest notificationQueryRequest) {
        if (notificationQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        // 自定义分页查询
        List<NotificationVO> notificationVOList = notificationMapper.getQuery(pageParam, notificationQueryRequest);
        if (notificationVOList == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询失败");
        }
        // 设置分页结果
        pageParam.setRecords(notificationVOList);
        return pageParam;
    }

    @Override
    public Page<NotificationVO> getQueryByAdmin(Page<NotificationVO> pageParam, NotificationQueryAdminRequest notificationQueryRequest) {
        // 自定义分页查询
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();

        // 添加 teacherId 为 0 的条件
        queryWrapper.eq("teacherId", 0);

        // 创建一个与 pageParam 相同的 Page 对象，但类型为 Notification
        Page<Notification> notificationPage = new Page<>(pageParam.getCurrent(), pageParam.getSize());

        // 执行分页查询
        Page<Notification> resultPage = this.page(notificationPage, queryWrapper);

        // 将查询结果转换为 NotificationVO 列表
        List<NotificationVO> notificationVOList = notificationPage.getRecords().stream().map(this::convertToNotificationVO).collect(Collectors.toList());

        // 设置分页结果
        pageParam.setRecords(notificationVOList);
        pageParam.setTotal(notificationPage.getTotal());

        return pageParam;
    }

    // 将 Notification 转换为 NotificationVO
    private NotificationVO convertToNotificationVO(Notification notification) {
        NotificationVO notificationVO = new NotificationVO();
        notificationVO.setId(notification.getId());
        notificationVO.setTitle(notification.getTitle());
        notificationVO.setContent(notification.getContent());
        notificationVO.setType(notification.getType());
        notificationVO.setCreateTime(notification.getCreateTime());
        notificationVO.setTeacherName("管理员");
        return notificationVO;
    }

}




